﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using BaseBll;
using BaseWebApi.App_Start;
using BaseWebApi.Common;
using BaseWebApi.Models;
using Common;

namespace BaseWebApi.Controllers
{
    /// <summary>
    /// 资产标签
    /// </summary>
    [ControllerGroup(groupName: "AssetTag", useage: "资产管理-资产标签")]
    public class AssetTagController : BaseController
    {
        private JWTPlayloadInfo playload;
        //private readonly AssetTagBll _instance = AssetTagBll.Instance;

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页尺寸</param>
        /// <param name="assetNo">资产编号</param>
        /// <param name="assetName">资产名称</param>
        /// <param name="deptId">部门id</param>
        /// <param name="RFID">RFID卡号</param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage GetPageList(int pageIndex = 1, int pageSize = 10, string assetNo = "", string assetName = "", int deptId = 0, string RFID = "")
        {
            StringBuilder strWhere = new StringBuilder("");
            if (!string.IsNullOrEmpty(assetNo))
            {
                strWhere.Append(" and AssetNo like '%" + assetNo + "%'");
            }
            if (!string.IsNullOrEmpty(assetName))
            {
                strWhere.Append(" and AssetName like '%" + assetName + "%'");
            }
            if (deptId != 0)
            {
                strWhere.Append(" and DeptID = " + deptId);
            }
            if (!string.IsNullOrEmpty(RFID))
            {
                strWhere.Append(" and RFIDNo like '%" + RFID + "%'");
            }

            DataTable dt = AssetInvBll.Instance.GetPageList(pageSize, pageIndex, strWhere.ToString(), "BuyDate DESC", out int records);
            int totalPages = PageHelper.GetPageCount(pageSize, records);
            return JsonHelper.SuccessJson(CommonOperation.DataTable2JArray(dt), pageIndex, totalPages, records);
        }

        /// <summary>
        /// 标签打印
        /// </summary>
        /// <param name="qrCodes"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage PrintMultipleQRCodes(List<string> qrCodes)
        {
            try
            {
                // 遍历每个二维码数据  
                foreach (var qrCodeData in qrCodes)
                {
                    PrintHelper.PrintQRCodeImage(qrCodeData);
                }

                // 所有二维码都已处理，返回成功响应  
                return JsonHelper.SuccessJson("标签已打印");
            }
            catch (Exception ex)
            {
                // 处理异常，并返回错误响应  
                throw;
            }
        }

        

    }
}
